package cn.itcast.nems.common.web.controller.b;

import cn.itcast.ic.common.bean.BizError;
import cn.itcast.ic.common.exception.BizException;
import cn.itcast.nems.base.dto.DictionaryDTO;
import cn.itcast.nems.base.service.DictionaryService;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.manager.icc.constant.CommonConstant;
import cn.itcast.nems.manager.util.RequestToEntityConverter;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 功能描述: 字典管理
 *
 * @author 陈早庆
 * @return
 * @update:[变更日期2023年12月13日][更改人姓名][变更描述]
 * @since 2023年12月13日
 */
@RestController
@RequestMapping("/b/dictionary")
@Slf4j
public class DictionaryController {
    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 字典分页查询
     * 用法：
     * {
     * "condition": {
     * "parent_code": "REASON_DESCRIPTION"
     * },
     * "page_number": 1,
     * "page_size": 30
     * }
     *
     * @param request 请求参数
     * @return ResponseEntity<?>
     **/
    @GetMapping
    public ResponseEntity<?> list(HttpServletRequest request, @RequestParam(value = "page_number", required = false) Integer pageNumber, @RequestParam(value = "page_size", required = false) Integer pageSize) {
        DictionaryDTO myEntity = (DictionaryDTO) RequestToEntityConverter.convert(request, DictionaryDTO.class);
//        BasePageRequest<DictionaryDTO> params = new BasePageRequest<>();
//        params.setCondition(myEntity);
//        params.setPageNumber(pageNumber);
//        params.setPageSize(pageSize);
        if (pageNumber != null && pageSize != null) {
            // 学籍流转原因
            return ResponseEntity.ok().body(dictionaryService.listPage(pageNumber, pageSize, myEntity));
        }
        return ResponseEntity.ok().body(dictionaryService.list(myEntity));
    }

    @GetMapping(value = "/{parentCode}")
    public ResponseEntity<?> list(@PathVariable("parentCode") String parentCode, HttpServletRequest request, @RequestParam(value = "page_number", required = false) Integer pageNumber, @RequestParam(value = "page_size", required = false) Integer pageSize) {
        DictionaryDTO myEntity = (DictionaryDTO) RequestToEntityConverter.convert(request, DictionaryDTO.class);
//        BasePageRequest<DictionaryDTO> params = new BasePageRequest<>();
//        params.setCondition(myEntity);
//        params.setPageNumber(pageNumber);
//        params.setPageSize(pageSize);
        myEntity.setParentCode(parentCode);
        if (pageNumber != null && pageSize != null) {
            // 学籍流转原因
            return ResponseEntity.ok().body(dictionaryService.list(pageNumber, pageSize, myEntity));
        }
        return ResponseEntity.ok().body(dictionaryService.list(myEntity));
    }

    /**
     * 新增字典
     *
     * @param params 参数对象
     * @return 是否成功
     **/
    @PostMapping()
    public ResponseEntity<Integer> create(@RequestBody DictionaryDTO params) {
        if (Objects.isNull(params) || params.getName().isEmpty()) {
            throw new BizException(BizError.PARAMETER_ERROR);
        }
        if (params.getName().length() > CommonConstant.DICTIONARY_NAME_LENGTH) {
            log.info("[新增字典] 参数超过40个字符");
            throw new BizException(BizError.PARAMETER_ERROR);
        }
        return ResponseEntity.ok().body(dictionaryService.create(params));
    }

    /**
     * 修改字典
     *
     * @param params 参数对象
     * @return 是否成功
     **/
    @PutMapping()
    public ResponseEntity<Boolean> update(@RequestBody DictionaryDTO params) {
        if (Objects.isNull(params) || params.getName().isEmpty() || Objects.isNull(params.getId())) {
            throw new BizException(BizError.PARAMETER_ERROR);
        }
        if (params.getName().length() > CommonConstant.DICTIONARY_NAME_LENGTH) {
            throw new BizException(BizError.PARAMETER_ERROR);
        }
        return ResponseEntity.ok().body(dictionaryService.update(params));
    }

    /**
     * 启用/禁用
     *
     * @param id    id
     * @param state 0未启用、1启用、8停用
     * @return 是否成功
     **/
    @PutMapping(value = "/{id}")
    public ResponseEntity<Boolean> updateState(@PathVariable("id") Integer id, @RequestParam(value = "state") Integer state) {
        return ResponseEntity.ok().body(dictionaryService.updateState(id, state));
    }

}
